package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.article.domain.FlightDestination;
import cn.wolfcode.luowowo.article.domain.FlightMessage;
import cn.wolfcode.luowowo.article.service.IFlightDestinationService;
import cn.wolfcode.luowowo.article.service.IFlightOrderService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.website.web.annotation.UserParam;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/flight")
public class FlightController {
    @Reference
    private IFlightDestinationService flightDestination;
    @Reference
    private IFlightOrderService flightOrderService;
    @RequestMapping("")
    public String fight(Model model) {
        //根据是否热门查询热门机场地区,hotFlights
        List<FlightDestination> hotFlights = flightDestination.getDestByIsHot();
        model.addAttribute("hotFlights", hotFlights);
        //根据首字母查A到E的地区 initialA
        List<Map<String, Object>> destByinitials = flightDestination.getDestByinitials2("A,B,C,D,E");
        model.addAttribute("initialA", destByinitials);
        //根据首字母查F到J的地区 initialF
        List<Map<String, Object>> initialF = flightDestination.getDestByinitials2("F,G,H,J");
        model.addAttribute("initialF", initialF);
        //根据首字母查K到P的地区 initialK
        List<Map<String, Object>> initialK = flightDestination.getDestByinitials2("K,L,M,N,P");
        model.addAttribute("initialK", initialK);
        //根据首字母查Q到W的地区 initialQ
        List<Map<String, Object>> initialQ = flightDestination.getDestByinitials2("Q,R,S,T,W");
        model.addAttribute("initialQ", initialQ);
        //根据首字母查X到Z的地区 initialX
        List<Map<String, Object>> initialX = flightDestination.getDestByinitials2("X,Y,Z");
        model.addAttribute("initialX", initialX);
        return "flight/index";
    }

 /*   @RequestMapping("/search")
    @ResponseBody
    public AjaxResult search(String orgCity, String dstCity, @DateTimeFormat(pattern = "yyyy-MM-dd") Date depTime){
        List<FlightMessage> messages = flightDestination.getFlightMessage(orgCity, dstCity, depTime);
        return AjaxResult.success().addData(messages);
    }*/
     @RequestMapping("/search")
     public String search(String orgCity, String dstCity, @DateTimeFormat(pattern = "yyyy-MM-dd") Date depTime,@DateTimeFormat(pattern = "yyyy-MM-dd")Date arrTime,Model model){
         List<FlightMessage> list = new ArrayList<>();
         List<FlightMessage> messages = flightDestination.getFlightMessage(orgCity, dstCity, depTime);
         if(messages.size()>0&&messages!=null) {
             for (FlightMessage message : messages) {
                 list.add(message);
             }
         }
         //根据前台有没有传入返回日期来判断是单程还是往返机票
         if(arrTime!=null){
             //如果不为空,那么就把返程的飞机票信息也查出来放到集合中
             List<FlightMessage> rs = flightDestination.getFlightMessage(dstCity, orgCity, arrTime);
             if(rs.size()>0&&rs!=null) {
                 for (FlightMessage message : rs) {
                     list.add(message);
                 }
             }
         }
         model.addAttribute("tickets",list);
         return "flight/flightInfo";
     }
     @RequestMapping("/order")
     @ResponseBody
     public Object order(Long fid, @UserParam UserInfo userInfo){
         if(userInfo==null){
             AjaxResult ajaxResult = new AjaxResult(false, "请先登录");
             return ajaxResult;
         }
         flightOrderService.saveOrder(fid,userInfo.getId());
         return AjaxResult.success();
     }
     @RequestMapping("/hello")
     public String hello(){
         return "ticket/addOrder";
     }
}
